{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python是动态的逐行解释运行\n",
    "当 .py 文件以模块形式被导入时， if __name__ == '__main__' 之下的代码块不被运行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['', 'C:\\\\Users\\\\闫厚镇\\\\Desktop\\\\datawhale学习\\\\python基础', 'E:\\\\conda\\\\4\\\\python37.zip', 'E:\\\\conda\\\\4\\\\DLLs', 'E:\\\\conda\\\\4\\\\lib', 'E:\\\\conda\\\\4', 'E:\\\\conda\\\\4\\\\lib\\\\site-packages', 'E:\\\\conda\\\\4\\\\lib\\\\site-packages\\\\win32', 'E:\\\\conda\\\\4\\\\lib\\\\site-packages\\\\win32\\\\lib', 'E:\\\\conda\\\\4\\\\lib\\\\site-packages\\\\Pythonwin', 'E:\\\\conda\\\\4\\\\lib\\\\site-packages\\\\IPython\\\\extensions', 'C:\\\\Users\\\\闫厚镇\\\\.ipython']\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print(sys.path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#练习题\n",
    "import collection\n",
    "language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "d={}\n",
    "for i in language:\n",
    "    d[i] = d.get(i,0) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "a= list(d.items())\n",
    "a.sort(key=lambda x:x[1],reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# datetime模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "import datetime\n",
    "datetime.now(tz=None) 获取当前的日期时间，输出顺序为：年、月、日、时、分、秒、微秒。\n",
    "datetime.timestamp() 获取以 1970年1月1日为起点记录的秒数。\n",
    "datetime.fromtimestamp(tz=None) 使用 unixtimestamp 创建一个 datetime。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-06-25 11:23:59\n",
      "1593055439.0\n",
      "2020-06-25 11:23:59\n",
      "2020-08-07 10:40:09.945965\n"
     ]
    }
   ],
   "source": [
    "dt = datetime.datetime(year=2020, month=6, day=25, hour=11, minute=23, second=59)\n",
    "print(dt) # 2020-06-25 11:23:59\n",
    "print(dt.timestamp())\n",
    "dt = datetime.datetime.fromtimestamp(1593055439.0)\n",
    "print(dt)\n",
    "dt = datetime.datetime.now()\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datetime.strftime(fmt)  格式化 datetime 对象，转换成自己想要的格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt = datetime.datetime(year=2020, month=6, day=25, hour=11, minute=51, second=49)\n",
    "s = dt.strftime(\"'%Y/%m/%d %H:%M:%S\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nov-28,2010\n"
     ]
    }
   ],
   "source": [
    "#如何将给定日期转换为 \"mmm-dd, YYYY\" 的格式？ 从标准的日期格式转\n",
    "d1 = datetime.date(2010,11,28)\n",
    "print(d1.strftime('%b-%d,%Y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-06-25 00:00:00\n"
     ]
    }
   ],
   "source": [
    "#从字符串格式转换   需要导入dateutil模块中的 parser包\n",
    "from dateutil import parser\n",
    "s = '2020-06-25'\n",
    "dt = parser.parse(s)\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\conda\\4\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: The pandas.datetime class is deprecated and will be removed from pandas in a future version. Import from datetime module instead.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "from pandas import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[datetime.timedelta(days=8) datetime.timedelta(days=5)\n",
      " datetime.timedelta(days=5) datetime.timedelta(days=3)]\n"
     ]
    }
   ],
   "source": [
    "#算连续的天数 一阶差分\n",
    "import numpy as np\n",
    "from dateutil import parser\n",
    "dateString = ['Oct, 2, 1869', 'Oct, 10, 1869', 'Oct, 15, 1869', 'Oct, 20, 1869', 'Oct, 23, 1869']\n",
    "dates = [parser.parse(i) for i in dateString] #第一步 解析为正常的格式\n",
    "td = np.diff(dates)#第二步 算差分\n",
    "print(td)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 5, 5, 3]\n"
     ]
    }
   ],
   "source": [
    "d = [i.days for i in td] #第三步 取天数\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-08-07\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "d = datetime.date.today()\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "273\n",
      "6\n",
      "40\n"
     ]
    }
   ],
   "source": [
    "#计算两个日期之间有多少个星期六\n",
    "import datetime\n",
    "d1 = datetime.date(1869, 1, 2)\n",
    "d2 = datetime.date(1869, 10, 2)\n",
    "dt = (d2 - d1).days\n",
    "print(dt)\n",
    "print(d1.isoweekday()) # 6 开头的一天是周几\n",
    "print(dt // 7+1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 10, 2, 0, 0)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将给定日期转换为当天开始的时间\n",
    "import datetime\n",
    "date = datetime.date(2019, 10, 2)\n",
    "dt = datetime.datetime(date.year, date.month, date.day)\n",
    "dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30 days, 0:00:00\n"
     ]
    }
   ],
   "source": [
    "#timedelta它常常被用来从 datetime 对象中添加或移除一段特定的时间。 日期的增减\n",
    "import datetime\n",
    "td = datetime.timedelta(days=30)\n",
    "print(td)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "365 days, 0:00:00\n"
     ]
    }
   ],
   "source": [
    "dt1 = datetime.datetime(2020, 1, 31, 10, 10, 0)\n",
    "dt2 = datetime.datetime(2019, 1, 31, 10, 10, 0)\n",
    "td = dt1 - dt2\n",
    "print(td)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23 days, 0:00:00\n"
     ]
    }
   ],
   "source": [
    "td1 = datetime.timedelta(days=30) # 30 days\n",
    "td2 = datetime.timedelta(weeks=1) # 1 week\n",
    "td = td1 - td2\n",
    "print(td)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1969-10-02\n",
      "2020-08-07\n",
      "2020-10-02\n",
      "18572\n",
      "56\n"
     ]
    }
   ],
   "source": [
    "from dateutil import parser\n",
    "import datetime\n",
    "bDay = 'Oct 2, 1969'\n",
    "dt1 = parser.parse(bDay).date()\n",
    "dt2 = datetime.date.today()\n",
    "dt3 = datetime.date(dt2.year, dt1.month, dt1.day)\n",
    "print(dt1) \n",
    "print(dt2) \n",
    "print(dt3) \n",
    "td = dt2 - dt1\n",
    "print(td.days) \n",
    "td = dt3 - dt2\n",
    "print(td.days) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-01-05\n",
      "2020-01-12\n",
      "2020-01-19\n",
      "2020-01-26\n",
      "2020-02-02\n",
      "2020-02-09\n",
      "2020-02-16\n",
      "2020-02-23\n",
      "2020-03-01\n",
      "2020-03-08\n",
      "2020-03-15\n",
      "2020-03-22\n",
      "2020-03-29\n",
      "2020-04-05\n",
      "2020-04-12\n",
      "2020-04-19\n",
      "2020-04-26\n",
      "2020-05-03\n",
      "2020-05-10\n",
      "2020-05-17\n",
      "2020-05-24\n",
      "2020-05-31\n",
      "2020-06-07\n",
      "2020-06-14\n",
      "2020-06-21\n",
      "2020-06-28\n",
      "2020-07-05\n",
      "2020-07-12\n",
      "2020-07-19\n",
      "2020-07-26\n",
      "2020-08-02\n",
      "2020-08-09\n",
      "2020-08-16\n",
      "2020-08-23\n",
      "2020-08-30\n",
      "2020-09-06\n",
      "2020-09-13\n",
      "2020-09-20\n",
      "2020-09-27\n",
      "2020-10-04\n",
      "2020-10-11\n",
      "2020-10-18\n",
      "2020-10-25\n",
      "2020-11-01\n",
      "2020-11-08\n",
      "2020-11-15\n",
      "2020-11-22\n",
      "2020-11-29\n",
      "2020-12-06\n",
      "2020-12-13\n",
      "2020-12-20\n",
      "2020-12-27\n"
     ]
    }
   ],
   "source": [
    "#练习题二\n",
    "import datetime   \n",
    "def all_sundays(year):\n",
    "    # your code here\n",
    "    dt1=datetime.date(int(year),1,1)\n",
    "    dt2=datetime.date(int(year),12,31)\n",
    "    days=(dt2-dt1).days\n",
    "    for i in range(days+1):\n",
    "    \tif (dt1+datetime.timedelta(days=i)).isoweekday()==7:\n",
    "    \t\tprint (dt1+datetime.timedelta(days=i))\n",
    "all_sundays('2020')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
